#!/usr/bin/python
# coding=utf-8
import commands
import os

TAG_APP_DOCTOR = "1"
TAG_APP_PATIENT = "2"
TAG_TASK_ASSEMBLE = "1"
TAG_TASK_INSTALL = "2"
SOURCE_DIR = "/home/shangsong/gerrit/android-patient/"
FLAVORS_FILE_PATH = SOURCE_DIR + "branch_dev_xinxianghos/YHT_Patient/flavors.gradle"
CONFIG_SIGNATURE_FILE = "/home/shangsong/work/build/signing.properties"
CONFIG_OUTPUT_DIR = "/home/shangsong/work/build/output/"

command_build_doctor = "./gradlew clean  :YHT_Doctor:%s" \
                       " --daemon --parallel-threads=4 --stacktrace " \
                       "-DsigntureFilePath=" + CONFIG_SIGNATURE_FILE + \
                       "  -DapkOutPutPath=" + CONFIG_OUTPUT_DIR

command_build_patient = "./gradlew clean -b branch_dev_xinxianghos/YHT_Patient/build.gradle" \
                        " %s --daemon --parallel-threads=4 --stacktrace " \
                        "-DsigntureFilePath=" + CONFIG_SIGNATURE_FILE + \
                        "  -DapkOutPutPath=" + CONFIG_OUTPUT_DIR + \
                        " -Dwhite_list=%s" +\
                        " -Dnest_nim_white_list=%s"

flavor_list = []


def build(app_tag, task_tag):
    # 编译医生端或者患者端
    os.chdir(SOURCE_DIR)
    if TAG_APP_DOCTOR == app_tag:
        build_doctor_app(task_tag)
    elif TAG_APP_PATIENT == app_tag:
        build_patient_app(task_tag)
    else:
        print 'Not defined'

    return


def build_doctor_app(task_tag):
    # 编译医生端
    if TAG_TASK_ASSEMBLE == task_tag:
        task_name = "assemble"
        command = command_build_doctor % task_name
        print "command: ", command

        (status, output) = commands.getstatusoutput(command)
        print "output: ", output

    elif TAG_TASK_INSTALL == task_tag:
        task_name = "installDebug"
        command = command_build_doctor % task_name
        print "command: ", command

        (status, output) = commands.getstatusoutput(command)
        print "output: ", output

    else:
        print "Not defined"
    return


def build_patient_app(task_tag):
    # 编译患者端端
    read_flavors(FLAVORS_FILE_PATH)
    flavor_list.sort()
    for flavor in flavor_list:
        print ("%d: %s" % (flavor_list.index(flavor), flavor))

    flavor_index = raw_input("请选择渠道：")
    selected_flavor = flavor_list[int(flavor_index)]
    print ("selected flavor：%s", selected_flavor)
    if TAG_TASK_ASSEMBLE == task_tag:
        task_name = "assemble" + flavor_list[int(flavor_index)].capitalize()
        command = command_build_patient % (task_name, selected_flavor, selected_flavor)
        print "command: ", command

        (status, output) = commands.getstatusoutput(command)
        print "output: ", output

    elif TAG_TASK_INSTALL == task_tag:
        task_name = "install" + flavor_list[int(flavor_index)].capitalize() + "Debug"
        command = command_build_patient % (task_name, selected_flavor, selected_flavor)
        print "command: ", command

        (status, output) = commands.getstatusoutput(command)
        print "output: ", output

    else:
        print "Not defined"
    return


def read_flavors(file_path):
    # 读取患者端所有渠道
    for line in open(file_path):
        if "{" in line:
            flavor = line.strip().lstrip().rstrip('{')
            flavor_list.append(flavor)


appTag = raw_input("编译医生端请输入" + TAG_APP_DOCTOR + "，编译患者端请输入" + TAG_APP_PATIENT + ": ")
taskTag = raw_input("请选择编译类型，assemble请输入" + TAG_TASK_ASSEMBLE + "，install请输入" + TAG_TASK_INSTALL + ": ")
build(appTag, taskTag)
